package com.polymer.app.test;

import java.util.concurrent.atomic.AtomicReference;

public class SpinLock {
    private static AtomicReference<Thread> cas = new AtomicReference<Thread>();
    public static void lock() {
        Thread current = Thread.currentThread();
        // 利用CAS
        while (!cas.compareAndSet(null, current)) {
            System.out.println(+1);
        }
        System.out.println(+2);
    }
    public void unlock() {
        Thread current = Thread.currentThread();
        cas.compareAndSet(current, null);
    }
    
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
             lock();
        }
       
    }
}